<?php

defined('BASEPATH') or die('forbidden');

class excelmulti_Controller Extends Controller {

    function _init() {
        check_level(50, segment(0), segment(1, 'index'));
        header("Content-type:text/html;charset=UTF-8"); 
        date_default_timezone_set('PRC'); 
    }
    function type_object()
    {
        $this->center_connect = $this->load->database('center_db');
        $sql="select id,name from dynamic_type_object";
        $this->center_connect->query($sql);
        unset($type_object);
        while ($m=$this->center_connect->get_array('default'))
        {
            $type_object[$m['name']]=$m['id'];
        }
        $this->center_connect->free('default');
        
        $this->type_db = $this->load->database('type_db');
        $sql="select id,name from type_object";
        $this->type_db->query($sql);
        while ($n = $this->type_db->get_array('default')) {
            $type_object[$n['name']]=$n['id'];
        }
        $this->type_db->free('default');
        return $type_object;
    }
    
    function index() 
    {       
        $type_object=$this->type_object();
        //print_r(count($type_object));
        $this->view->display("freegift/excelmulti/index");
    }
    function flexigrid() {
        //读取flexigrid传输的数据
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $path=MULTIUPLOADPATH.'excel/award/';
        $filearray=get_filenamesbydir($path,2);
        $total=count($filearray);
        for ($i=$offset;$i<$total;$i++) {
            $n['type']      = '奖励发放';
            $n['intime']    =date('Y-m-d H:i:s',$filearray[$i]['time']);
            $n['filename']  =$filearray[$i]['filename'];
            $rows[] = array('id' => $n['filename'], 'cell' => ($n));
        }
        
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }
    function add()
    {
        if(is_submit())
        {
            if($_FILES['file']['error']>0)
            {
                echo "Error: " . $_FILES["file"]["error"] . "<br />";
            }
            else
            {
                $filetype   =$_FILES['file']['type'];
                
                if($filetype=='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
                {
                    $tmp_name   =$_FILES['file']['tmp_name'];
                    $file_name  =$_FILES['file']['name'];
                    $awardpath       =MULTIUPLOADPATH.'excel/award/'.$file_name;      
                    if(file_exists($awardpath))
                    {
                        unlink($awardpath);
                    }
                    move_uploaded_file($tmp_name, $awardpath);
                    $newfilename=MULTIUPLOADPATH.'excel/award/award'.date('YmdHis',time()).'.xlsx';     
                    //$_SESSION['multiroleaward']=$newfilename;
                    $renameresult=rename($awardpath, $newfilename);
                    if($renameresult)
                    {
                        //$message='upload file success,Sending award……';
                        //echo "<script>alert('".$message."');</script>";
                        $type_object=$this->type_object();   
                        $area_host  =  get_area_list_url();
                        $excelarray =  readExcel($newfilename);            
                        for($i=2;$i<=count($excelarray);$i++)
                        {
                            $area_url                                  =$area_host[$excelarray[$i]['A']];
                            $awardarray[$area_url][0]['area_url']      =$area_url;                
                            $awardarray[$area_url][$i]['name']         =$excelarray[$i]['B'];
                            $awardarray[$area_url][$i]['subject']      =$excelarray[$i]['C'];
                            $awardarray[$area_url][$i]['content']      =$excelarray[$i]['D'];
                            $item1                                     =  explode(':', $excelarray[$i]['E']);
                            $item2                                     =  explode(':', $excelarray[$i]['F']);
                            $item3                                     =  explode(':', $excelarray[$i]['G']);
                            $item4                                     =  explode(':', $excelarray[$i]['H']);
                            $awardarray[$area_url][$i]['item1']        =$type_object[$item1[0]];
                            $awardarray[$area_url][$i]['num1']         =(int)$item1[1];
                            $awardarray[$area_url][$i]['item2']        =$type_object[$item2[0]];
                            $awardarray[$area_url][$i]['num2']         =(int)$item2[1];
                            $awardarray[$area_url][$i]['item3']        =$type_object[$item3[0]];
                            $awardarray[$area_url][$i]['num3']         =(int)$item3[1];
                            $awardarray[$area_url][$i]['item4']        =$type_object[$item4[0]];
                            $awardarray[$area_url][$i]['num4']         =(int)$item4[1];
                        }
                        unset($excelarray);
                        unset($area_host);
                        $awardarray=  array_values($awardarray);
                        $area_host=  get_area_list_host();
                        $errorStr='';
                        for($j=0;$j<count($awardarray);$j++)
                        {
                            $area_url=$awardarray[$j][0]['area_url'];
                            unset($awardarray[$j][0]);
                            unset($jsonarray);
                            $jsonarray=array_values($awardarray[$j]);
                            $json=json_encode($jsonarray);
                            $url=$area_url.'json_str='.$json.'&is_same_content=0'.$_POST['is_same_content'].'&action=42';
                            $strResult[$area_url]=  vpost($url);
                            if ($strResult[$area_url] === FALSE)
                                $errorStr .= $area_host[$area_url] . translate('C_TIPS_ERROR_TEXT') . "\r\n";             
                        }
                        unset($awardarray);
                        if (empty($errorStr))
                            $result = "success";
                        else
                            $result=$errorStr;
                        echo "<script>alert('".$result."');</script>";
                    }
                    else {
                        $message='fail upload file';
                        echo "<script>alert('".$message."');</script>";
                    }                                       
                }
                else
                {
                    $message='please upload Excel2007 file!';
                    echo "<script>alert('".$message."');</script>";
                }                                
            }
        }         
        $this->view->display("freegift/excelmulti/add");
    }
    function edit($id)
    {
        $excelpath  =  MULTIUPLOADPATH.'excel/award/'.$id;      
        $excelarray =  readExcel($excelpath);
        for($i=1;$i<=count($excelarray);$i++)
        {
            if($i==1)
            {
                $string="<tr bgcolor='#cccfff'>
                            <th width='60px'>{$excelarray[$i]['A']}</th>
                            <th>{$excelarray[$i]['B']}</th>
                            <th>{$excelarray[$i]['C']}</th>
                            <th>{$excelarray[$i]['D']}</th>
                            <th>{$excelarray[$i]['E']}</th>
                            <th>{$excelarray[$i]['F']}</th>
                            <th>{$excelarray[$i]['G']}</th>
                            <th>{$excelarray[$i]['H']}</th>
                         </tr>";
            }
            else 
           {
               $string.="<tr bgcolor='#ffffff'>
                            <td>{$excelarray[$i]['A']}</td>
                            <td>{$excelarray[$i]['B']}</td>
                            <td>{$excelarray[$i]['C']}</td>
                            <td>{$excelarray[$i]['D']}</td>
                            <td>{$excelarray[$i]['E']}</td>
                            <td>{$excelarray[$i]['F']}</td>
                            <td>{$excelarray[$i]['G']}</td>
                            <td>{$excelarray[$i]['H']}</td>
                         </tr>";
           }
        }
        $this->view->assign('string',$string);
        $this->view->display("freegift/excelmulti/edit");
    }
}